package org.test.threadlocal;

/**
 * Created by Hy on 2018/7/11.
 * ThreadLocal 的API
 *  1).public void set(T value):将值放入线程局部变量中
 *  2).public T get():从线程局部变量中获取值；
 *  3).public void remove():从线程局部变量中移除值（有助于JVM垃圾回收）
 *  4).protected T initialValue():返回线程局部变量中的初始值（默认为null）
 */
public class SequenceB implements Sequence{
    private static ThreadLocal<Integer> numberContainer = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public int getNumber() {
        numberContainer.set(numberContainer.get()+1);
        return numberContainer.get();
    }

    public static void main(String[] args) {
        Sequence sequence = new SequenceB();
        ClientThread thread1 = new ClientThread(sequence);
        ClientThread thread2 = new ClientThread(sequence);
        ClientThread thread3 = new ClientThread(sequence);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
